package com.liushuang.waiwaiim.model;

import android.content.Context;
import android.content.Intent;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.hyphenate.EMContactListener;
import com.hyphenate.chat.EMClient;
import com.liushuang.waiwaiim.model.bean.InvationInfo;
import com.liushuang.waiwaiim.model.bean.UserInfo;
import com.liushuang.waiwaiim.utils.Constant;
import com.liushuang.waiwaiim.utils.SpUtils;

//全局事件监听器
public class EventListener {

    private Context mContext;
    private LocalBroadcastManager mLocalBroadcastManager;

    public EventListener(Context context){

        mContext = context;

        //创建一个发送广播的管理者对象
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(mContext);

        //注册一个联系人变化的监听
        EMClient.getInstance().contactManager().setContactListener(mEMContactListener);
    }

    private EMContactListener mEMContactListener = new EMContactListener() {
        //联系人增加后执行的方法
        @Override
        public void onContactAdded(String hxid) {
            //数据更新
            Model.getInstance().getDbManager().getContactTableDao().saveContact(new UserInfo(hxid), true);

            //发送联系人变化的广播
            mLocalBroadcastManager.sendBroadcast(new Intent(Constant.CONTACT_CHANGED));
        }

        //联系人删除后执行的方法
        @Override
        public void onContactDeleted(String hxid) {

            Model.getInstance().getDbManager().getContactTableDao().deleteContactByHxId(hxid);
            Model.getInstance().getDbManager().getInviteTableDao().removeInvitation(hxid);

            mLocalBroadcastManager.sendBroadcast(new Intent(Constant.CONTACT_CHANGED));

        }

        //接收到别人的邀请
        @Override
        public void onContactInvited(String hxid, String reason) {

            InvationInfo invationInfo = new InvationInfo();
            invationInfo.setUser(new UserInfo(hxid));
            invationInfo.setReason(reason);
            invationInfo.setStatus(InvationInfo.InvitationStatus.NEW_INVITE); //新的邀请

            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invationInfo);

            //当别人对你发起邀请时，会出现红点提示
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE, true);

            //发送邀请信息变化的广播
            mLocalBroadcastManager.sendBroadcast(new Intent(Constant.CONTACT_INVITE_CHANGED));
        }

        //别人同意了你的好友邀请
        @Override
        public void onFriendRequestAccepted(String hxid) {

            InvationInfo invationInfo = new InvationInfo();
            invationInfo.setUser(new UserInfo(hxid));
            invationInfo.setStatus(InvationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER); //邀请被接受

            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invationInfo);

            //当别人同意了你的邀请时，会有红点提醒
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE, true);

            mLocalBroadcastManager.sendBroadcast(new Intent(Constant.CONTACT_INVITE_CHANGED));

        }

        //别人拒绝了你的好友邀请
        @Override
        public void onFriendRequestDeclined(String username) {

            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE, true);

            mLocalBroadcastManager.sendBroadcast(new Intent(Constant.CONTACT_INVITE_CHANGED));
        }
    };
}
